import { PropDt } from "../../../Framework/Common/CommonInterface";
import { levelDtMgr, propDtMgr } from "../../../Framework/Data/DataBaseMgr";
import DataMgr from "../../../Framework/Data/DataMgr";
import ResMgr from "../../../Framework/Res/ResMgr";
import DeepClone from "../../Utils/DeepClone";
import Player from "../Object/Player";
import Prop from "../Object/Prop";
import { EffectBase, getEffect } from "../Object/PropEffect";
import ModuleBase from "./ModuleBase";
import ModuleMgr from "./ModuleMgr";
import { PoolManager } from "./NodePool";
import TiledMapCtrl from "./TiledMapCtrl";

const { ccclass, property } = cc._decorator;
@ccclass
export default class PropLayer extends ModuleBase {
    private _levelDtMgr: levelDtMgr = null;
    private _propDtMgr: propDtMgr = null;
    private _tiledMapCtrl: TiledMapCtrl = null;
    private _arrProp: Prop[] = [];
    private _atlas: cc.SpriteAtlas = null;

    async onInit(...rest: any[]) {
        this._levelDtMgr = DataMgr.instance.getMgr("levelDtMgr");
        this._propDtMgr = DataMgr.instance.getMgr("propDtMgr");
        this._tiledMapCtrl = ModuleMgr.instance.getModule<TiledMapCtrl>("TiledMapCtrl");
        let arrTilePos = this._tiledMapCtrl.getAllTiledPosSetProp();
        // 使用专门的loadAtlasRes方法替代getRes
        this._atlas = await ResMgr.instance.loadAtlasRes("AtlasRes", "prop");
        this.addProp(arrTilePos);
    }

    addProp(arrTiledPos: cc.Vec2[]) {
        //获取关卡数据
        let levelDt = this._levelDtMgr.getDataByCurIndex();
        let objProp = levelDt["prop"];
        for (let propId in objProp) {
            let propNum = objProp[propId] - 2 + Math.floor(Math.random() + 4);
            let propIdInt = parseInt(propId);
            let propDt = DeepClone.deepClone(this._propDtMgr.getDataByID(propIdInt)) as PropDt;
            let imgName = propDt["img"];
            for (let i = 0; i < propNum; i++) {
                let index = Math.floor(Math.random() * arrTiledPos.length);
                let tile = arrTiledPos[index];
                let pos = this._tiledMapCtrl.getPoxiByTiledPos(tile);
                arrTiledPos.splice(index, 1);
                let propNode = PoolManager.instance.getNodePool("Prop").getNode();
                propNode.parent = this._tiledMapCtrl.node;
                propNode.active = false;
                propNode.setPosition(pos);
                // propNode.zIndex = 100;
                let spriteComp = propNode.getComponent(cc.Sprite);
                let newSpriteFrame = this._atlas.getSpriteFrame(imgName);
                spriteComp.spriteFrame = newSpriteFrame;
                let propTs = propNode.getComponent(Prop);
                propTs.propTiledPos = tile;
                propTs.propDt = propDt;
                this._arrProp.push(propTs);
                propTs.onInit();
                propNode.zIndex = (tile.y + 1);
            }
        }
    }
    refreshProp(oldTiledPos: cc.Vec2, newTiledPos: cc.Vec2) {
        let propTs = this.getPropByTiledPos(oldTiledPos);
        //只有道具未激活(隐藏在障碍物里)才需要移动，否则就不需要移动
        if (propTs && propTs.node.active == false) {
            propTs.stopMove();
            let pos = this._tiledMapCtrl.getPoxiByTiledPos(newTiledPos);
            propTs.node.setPosition(pos);
            propTs.startMove();
        }

    }
    getPropByTiledPos(tiledPos: cc.Vec2) {
        for (let prop of this._arrProp) {
            if (tiledPos.equals(prop.propTiledPos)) {
                return prop;
            }
        }
        return null;
    }

    //使用特效
    playEffect(propTs: Prop, target: Player) {
        //获取道具
        if (propTs.node.active) {
            let id = propTs.propDt.id;
            let value = propTs.propDt.value;
            let effect: EffectBase = getEffect(id, value, target);
            return effect.playerEffect();
        }
    }
    //移除道具
    removeProp(tiledPos: cc.Vec2) {
        let propTs = this.getPropByTiledPos(tiledPos);
        if (propTs) {
            for (let i = 0; i < this._arrProp.length; i++) {
                if (propTs == this._arrProp[i]) {
                    this._arrProp.splice(i, 1);
                    PoolManager.instance.getNodePool("Prop").putNode(propTs.node);
                    return;
                }
            }
        }
    }
}